package org.springblade.pm.wrappper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.pm.entity.Risk;
import org.springblade.pm.entity.XmglProject;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.vo.RiskVo;
import org.springblade.system.feign.IDictClient;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.user.feign.IUserClient;

import java.util.ArrayList;
import java.util.List;

public class RiskWrapper extends BaseEntityWrapper<Risk,RiskVo> {

	static ISysClient sysClient;
	static IDictClient dictClient;
	static IUserClient userClient;
	static IXmglProjectService xmglProjectService;
	static {

		sysClient = SpringUtil.getBean(ISysClient.class);
		dictClient = SpringUtil.getBean(IDictClient.class);
		userClient = SpringUtil.getBean(IUserClient.class);
		xmglProjectService = SpringUtil.getBean(IXmglProjectService.class);
	}

	public static RiskWrapper build() {
		return new RiskWrapper();
	}

	@Override
	public RiskVo entityVO(Risk entity) {
		RiskVo pmRiskVo= BeanUtil.copy(entity,RiskVo.class);
		//设置风险类别名称
		R<String> categoryName=dictClient.getValue("risk_category",entity.getCategory());
		if (categoryName.isSuccess()) {
			pmRiskVo.setCategoryName(categoryName.getData());
		}
		//设置风险可能发生阶段名称
		R<String> possibleStageName=dictClient.getValue("possible_stage",entity.getPossibleStage());
		if (possibleStageName.isSuccess()) {
			pmRiskVo.setPossibleStageName(possibleStageName.getData());
		}
		//设置应对策略
		R<String> countermeasureName=dictClient.getValue("preventive_solution",entity.getCountermeasure());
		if (countermeasureName.isSuccess()) {
			pmRiskVo.setCountermeasureName(countermeasureName.getData());
		}
		//设置风险状态名称
		R<String> statusName=dictClient.getValue("risk_status",entity.getStatus());
		if (statusName.isSuccess()) {
			pmRiskVo.setStatusName(statusName.getData());
		}
		//设置缓解措施执行情况
		R<String> implementationName=dictClient.getValue("implementation",entity.getImplementation());
		if (implementationName.isSuccess()) {
			pmRiskVo.setImplementationName(implementationName.getData());
		}
		//设置风险属性
		R<String> attributeName=dictClient.getValue("risk_attributes",entity.getAttribute());
		if (attributeName.isSuccess()) {
			pmRiskVo.setAttributeName(attributeName.getData());
		}

		if (entity.getProjectId()!=null){
			XmglProject project = xmglProjectService.getById(entity.getProjectId());
			pmRiskVo.setProjectName(project.getName());
			pmRiskVo.setProjectCode(project.getCode());
		}



		return pmRiskVo;

		}
	public List<RiskVo> entityVOList(List<Risk> list) {
		List<RiskVo> riskVoList = new ArrayList<>();
		for (Risk risk : list) {
			riskVoList.add(entityVO(risk));
		}
		return  riskVoList;
	}
}
